<template>
  <div class="login-page">
    <div class="login-container">
      <h2>{{ isH5() ? 'H5登录' : 'Web登录' }}</h2>
      <template v-if="isH5()">
        <!-- H5 登录表单 -->
        <van-form @submit="onSubmit">
          <van-field
            v-model="loginForm.username"
            name="username"
            label="用户名"
            placeholder="请输入用户名"
            :rules="[{ required: true, message: '请输入用户名' }]"
          />
          <van-field
            v-model="loginForm.password"
            type="password"
            name="password"
            label="密码"
            placeholder="请输入密码"
            :rules="[{ required: true, message: '请输入密码' }]"
          />
          <div style="margin: 16px;">
            <van-button round block type="primary" native-type="submit">
              登录
            </van-button>
          </div>
        </van-form>
      </template>
      <template v-else>
        <!-- Web 登录表单 -->
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="rules"
          label-width="0"
          class="login-form"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              prefix-icon="User"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              prefix-icon="Lock"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" @click="onSubmit" class="submit-btn">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </template>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { isH5 } from '@/utils/device';
import { storage } from '@/utils';
import type { FormInstance } from 'element-plus';

const router = useRouter();
const route = useRoute();
const loading = ref(false);
const loginFormRef = ref<FormInstance>();

const loginForm = reactive({
  username: '',
  password: ''
});

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
};

const onSubmit = async () => {
  if (!isH5() && loginFormRef.value) {
    await loginFormRef.value.validate();
  }
  
  loading.value = true;
  try {
    // 这里调用登录接口
    // const res = await login(loginForm);
    // 模拟登录成功
    const token = 'mock_token';
    storage.set('token', token);
    storage.set('userInfo', { username: loginForm.username });
    
    const redirect = route.query.redirect as string || '/';
    router.push(redirect);
  } catch (error) {
    console.error('登录失败:', error);
  } finally {
    loading.value = false;
  }
};
</script>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;

  .login-container {
    width: 100%;
    max-width: 400px;
    padding: 20px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

    h2 {
      text-align: center;
      margin-bottom: 24px;
      color: #303133;
    }
  }
}

.login-form {
  .submit-btn {
    width: 100%;
  }
}

// H5 样式适配
:deep(.van-field) {
  padding: 10px 16px;
}
</style> 